package com.bric.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

import com.bric.util.StaticStr;

public class FtpFile {
	private static final Logger log=Logger.getLogger(FtpFile.class);
	public static File download() {
		log.info(new Date() + "  开始从"+StaticStr.ftpIp+"处进行ftp数据下载");

		// ftp服务器登录凭证
		String host = StaticStr.ftpIp;
		int port = Integer.parseInt(StaticStr.ftpPort);
		String user = StaticStr.ftpUser;
		String password = StaticStr.ftpPasswd;

		// 获取时间字段信息
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String today1 = sdf1.format(date);
		String today = sdf.format(date);
		File localFile=null;

		String txtFileDir = StaticStr.ftpRoot;
		String txtSaveDir = StaticStr.localTempPath;

		/*
		 * //检查本地磁盘目录是否存在txt文件 boolean flag = isTxtExit(today1,txtSaveDir);
		 * log.info(new Date()+"  判断txt文件是否存在："+flag);
		 * FlagUtil.ftpDownloadRunning=true;
		 */

		// 讲txt的下载操作和解析操作分成2个独立的操作进行，排除互相间的干扰
		// if(flag==false)//文件不存在进行ftp下载操作
		// {
		FTPClient ftp = null;
		try {
			// ftp的数据下载
			ftp = new FTPClient();
			ftp.connect(host, port);
			ftp.login(user, password);
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

			// 设置linux环境
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
			ftp.configure(conf);

			// 判断是否连接成功
			int reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				log.info("FTP server refused connection.");
				return null;
			}

			// 设置访问被动模式
			ftp.setRemoteVerificationEnabled(false);
			ftp.enterLocalPassiveMode();

			// 检索ftp目录下所有的文件，利用时间字符串进行过滤
			boolean dir = ftp.changeWorkingDirectory(txtFileDir);
			if (dir) {
				FTPFile[] fs = ftp.listFiles();
				for (FTPFile f : fs) {
					if(f.getName().equals(StaticStr.ftpfileName)){
//					if (f.getName().indexOf(today1 + "2000") > 0) {
						File f1 = new File(txtSaveDir);
						if (!f1.exists()) {
							f1.mkdirs();
						}
						localFile = new File(txtSaveDir + f.getName());
						OutputStream ios = new FileOutputStream(localFile);
						ftp.retrieveFile(f.getName(), ios);
						ios.close();
						break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info(new Date() + "  ftp下载txt文件发生错误");
		} finally {
			if (ftp != null)
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
		}
		return localFile;
		// }
	}

/*	public static void main(String[] args) {
		File f=download();
		try {
			ZipCompressorByAnt.unzip(f.getAbsolutePath(), "E:/data", false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
}
